package com.rjwh.dingdong.client.util;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.community.CommunityPostDetailActivity;
import com.rjwh.dingdong.client.activity.community.CommunityPostMyMainPageActivity;
import com.rjwh.dingdong.client.activity.community.CommunityPostPublishActivity;
import com.rjwh.dingdong.client.activity.download.DownloadMangerActivity;
import com.rjwh.dingdong.client.activity.drawbook.DrawBookCatalogMainActivity;
import com.rjwh.dingdong.client.activity.drawbook.DrawBookDetailActivity;
import com.rjwh.dingdong.client.activity.homework.HomeWorkActivity;
import com.rjwh.dingdong.client.activity.homework.HomeWorkRankActivity;
import com.rjwh.dingdong.client.activity.image.ImageListBrowseActivity;
import com.rjwh.dingdong.client.activity.loginregist.ThirdPartyLoginActivity;
import com.rjwh.dingdong.client.activity.mathgame.MathGameActivity;
import com.rjwh.dingdong.client.activity.mathgame.MathGameDetailActivity;
import com.rjwh.dingdong.client.activity.message.MessageListActivity;
import com.rjwh.dingdong.client.activity.more.MoreAboutUsActivity;
import com.rjwh.dingdong.client.activity.more.MoreCollectionActivity;
import com.rjwh.dingdong.client.activity.more.MorePointsActivity;
import com.rjwh.dingdong.client.activity.radio.RadioAlbumListActivity;
import com.rjwh.dingdong.client.activity.radio.RadioAlbumProgramActivity;
import com.rjwh.dingdong.client.activity.radio.RadioAnchorHomePageActivity;
import com.rjwh.dingdong.client.activity.radio.RadioAnchorListActivity;
import com.rjwh.dingdong.client.activity.radio.RadioChildMusicListActivity;
import com.rjwh.dingdong.client.activity.webbrowser.IntegralMallActivity;
import com.rjwh.dingdong.client.activity.webbrowser.OaGameActivity;
import com.rjwh.dingdong.client.activity.webbrowser.WebActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.LocalGmaeInfo;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.service.HttpWebCoreService;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static android.R.attr.category;


/**
 * @author Kay
 * @ClassName UrlUtil
 * @Description url工具类
 * @date 2016-10-26
 * @history 2016-10-26 author: description:
 */

public class UrlUtil {
    /**
     * 处理actionurl的方法
     *
     * @param context   context
     * @param actionurl actionurl
     * @param isAction  是否执行跳转
     * @return Intent用于接收推送时的跳转
     */
    public static Intent handelUrl(Context context, String actionurl, boolean isAction) {
        boolean isLogined = MyApplication.spUtil.getBoolPreferenceByParamName(context,
                LocalConstant.SP_IS_LOGINED);
        Intent intent = null;
        if (AsStrUtils.isEmpty(actionurl)) {
            return null;
        }
        LogUtil.d("UrlUtil", "actionurl: " + actionurl);
        String decode = actionurl;
        try {
            decode = URLDecoder.decode(actionurl, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        LogUtil.d("UrlUtil", "decode: " + decode);
        if (actionurl.contains(LocalConstant.ACTIONURL_TIANDILIXIN)) {
            try {
                Uri uri = Uri.parse(decode);
                LogUtil.d("UrlUtil", "uri:" + uri);
                if (uri == null) {
                    return null;
                }
                Set<String> queryParameterNames = uri.getQueryParameterNames();
                if (queryParameterNames != null && !queryParameterNames.isEmpty() && queryParameterNames.contains(LocalConstant.ACTIONURL_TYPE)) {
                    String type = uri.getQueryParameter(LocalConstant.ACTIONURL_TYPE);
                    if (type == null) {
                        return null;
                    }
                    switch (type) {
                        case LocalConstant.ACTIONURL_ACTIVITY:
                            if (queryParameterNames.contains(LocalConstant.ACTIONURL_URL)) {
                                String url = uri.getQueryParameter(LocalConstant.ACTIONURL_URL);
                                if (url == null) {
                                    return null;
                                }
                                String webUrl = getWebUrl(context, url);
                                LogUtil.d("UrlUtil", "webUrl: " + webUrl);
                                if (webUrl == null) {
                                    return null;
                                }
                                if (queryParameterNames.contains(LocalConstant.ACTIONURL_AUTH)) {
                                    String auth = uri.getQueryParameter(LocalConstant.ACTIONURL_AUTH);
                                    if (auth == null) {
                                        return null;
                                    }
                                    if (auth.equals("1")) {
                                        //需要登录
                                        if (MyApplication.spUtil.getBoolPreferenceByParamName(context, LocalConstant.SP_IS_LOGINED)) {
                                            intent = new Intent(context, WebActivity.class);
                                            intent.putExtra("url", webUrl);
//                                            WebActivity.startActivity(context, webUrl);
                                        } else {
                                            intent = new Intent(context, ThirdPartyLoginActivity.class);
//                                            ThirdPartyLoginActivity.startActivity(context);
                                        }
                                    } else {
                                        intent = new Intent(context, WebActivity.class);
                                        intent.putExtra("url", webUrl);
//                                        WebActivity.startActivity(context, webUrl);
                                    }
                                } else {
                                    intent = new Intent(context, WebActivity.class);
                                    intent.putExtra("url", webUrl);
//                                    WebActivity.startActivity(context, webUrl);
                                }
                            }
                            break;
                        case LocalConstant.ACTIONURL_PICTURE:
                            if (queryParameterNames.contains(LocalConstant.ACTIONURL_SUBTYPE)) {
                                String subtype = uri.getQueryParameter(LocalConstant.ACTIONURL_SUBTYPE);
                                if (subtype == null) {
                                    return null;
                                }
                                switch (subtype) {
                                    case LocalConstant.ACTIONURL_LIBRARY:
                                        //绘本馆
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_CATEGORY)) {
                                            String category = uri.getQueryParameter(LocalConstant.ACTIONURL_CATEGORY);
                                            if (category == null) {
                                                return null;
                                            }
                                            switch (category) {
                                                case LocalConstant.ACTIONURL_UNIVERSAL:
                                                    //全部
                                                    intent = new Intent(context, DrawBookCatalogMainActivity.class);
                                                    intent.putExtra("position", 0);
//                                                DrawBookCatalogMainActivity.startActivity(context, 0);
                                                    break;
                                                case LocalConstant.ACTIONURL_SINOLOGY:
                                                    //国学绘本
                                                    intent = new Intent(context, DrawBookCatalogMainActivity.class);
                                                    intent.putExtra("position", 0);
//                                                DrawBookCatalogMainActivity.startActivity(context, 0);
                                                    break;
                                                case LocalConstant.ACTIONURL_CLASSIC:
                                                    //分级阅读
                                                    intent = new Intent(context, DrawBookCatalogMainActivity.class);
                                                    intent.putExtra("position", 1);
//                                                DrawBookCatalogMainActivity.startActivity(context, 1);
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_DETAIL:
                                        //绘本详情
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_BOOKID)) {
                                            String bookid = uri.getQueryParameter(LocalConstant.ACTIONURL_BOOKID);
                                            if (bookid == null) {
                                                return null;
                                            }
                                            intent = new Intent(context, DrawBookDetailActivity.class);
                                            intent.putExtra("bookid", bookid);
//                                        DrawBookDetailActivity.startActivity(context, bookid);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_COCOS:
                                        //进入绘本
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_BOOKID)) {
                                            String bookid = uri.getQueryParameter(LocalConstant.ACTIONURL_BOOKID);
                                            if (bookid == null) {
                                                return null;
                                            }
                                            startDrawBook(context, bookid);
//                                            intent = new Intent(context, DrawBookDetailActivity.class);
//                                            intent.putExtra("bookid", bookid);
//                                        DrawBookDetailActivity.startActivity(context, bookid);
                                        }
                                        break;

                                    default:
                                        break;
                                }
                            }
                            break;
                        case LocalConstant.ACTIONURL_MATH:
                            if (queryParameterNames.contains(LocalConstant.ACTIONURL_SUBTYPE)) {
                                String subtype = uri.getQueryParameter(LocalConstant.ACTIONURL_SUBTYPE);
                                if (subtype == null) {
                                    return null;
                                }
                                switch (subtype) {
                                    case LocalConstant.ACTIONURL_LIBRARY:
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_CATEGORY)) {
                                            String category = uri.getQueryParameter(LocalConstant.ACTIONURL_CATEGORY);
                                            if (category == null) {
                                                return null;
                                            }
                                            switch (category) {
                                                case LocalConstant.ACTIONURL_UNIVERSAL:
                                                    intent = new Intent(context, MathGameActivity.class);
//                                                    MathGameActivity.startActivity(context);
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_DETAIL:
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_BOOKID)) {
                                            String bookid = uri.getQueryParameter(LocalConstant.ACTIONURL_BOOKID);
                                            if (bookid == null) {
                                                return null;
                                            }
                                            intent = new Intent(context, MathGameDetailActivity.class);
                                            intent.putExtra("bookid", bookid);
//                                            MathGameDetailActivity.startActivity(context, bookid);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_COCOS:
                                        //进入绘本
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_BOOKID) && queryParameterNames.contains(LocalConstant.ACTIONURL_LESSONID)) {
                                            String bookid = uri.getQueryParameter(LocalConstant.ACTIONURL_BOOKID);
                                            String lessonid = uri.getQueryParameter(LocalConstant.ACTIONURL_LESSONID);
                                            if (bookid == null || lessonid == null) {
                                                return null;
                                            }
                                            startGame(context, bookid, lessonid);
//                                            intent = new Intent(context, DrawBookDetailActivity.class);
//                                            intent.putExtra("bookid", bookid);
//                                        MathGameDetailActivity.startActivity(context, bookid);
                                        }
                                        break;
                                }
                            }
                            break;
                        case LocalConstant.ACTIONURL_STORY:
                            if (queryParameterNames.contains(LocalConstant.ACTIONURL_SUBTYPE)) {
                                String subtype = uri.getQueryParameter(LocalConstant.ACTIONURL_SUBTYPE);
                                if (subtype == null) {
                                    return null;
                                }
                                switch (subtype) {
                                    case LocalConstant.ACTIONURL_ANCHORLIST:
                                        //主播列表
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_CATEGORY)) {
                                            String category = uri.getQueryParameter(LocalConstant.ACTIONURL_CATEGORY);
                                            if (category == null) {
                                                return null;
                                            }
                                            intent = new Intent(context, RadioAnchorListActivity.class);
                                            switch (category) {
                                                case LocalConstant.ACTIONURL_OWNER:
                                                    //跳转自有主播电台列表
                                                    intent.putExtra("category", LocalConstant.ACTIONURL_OWNER);
                                                    intent.putExtra("categoryName", "主播电台");

                                                    break;
                                                case LocalConstant.ACTIONURL_CREATER:
                                                    //跳转创客频道列表
                                                    intent.putExtra("category", LocalConstant.ACTIONURL_CREATER);
                                                    intent.putExtra("categoryName", "创客频道");

                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_ALBUM:
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_ALBUMID) && queryParameterNames.contains(LocalConstant.ACTIONURL_ANCHORID)) {
                                            //跳转主播节目主页（故事列表页）
                                            String albumid = uri.getQueryParameter(LocalConstant.ACTIONURL_ALBUMID);
                                            String anchorid = uri.getQueryParameter(LocalConstant.ACTIONURL_ANCHORID);
                                            if (anchorid == null || albumid == null) {
                                                return null;
                                            }
                                            if (!anchorid.equals("0")) {
                                                //主播节目列表
                                                intent = new Intent(context, RadioAlbumProgramActivity.class);
                                            } else {
                                                //跳转儿歌节目列表页
                                                intent = new Intent(context, RadioChildMusicListActivity.class);
                                            }
                                            intent.putExtra("anchorid", anchorid);
                                            intent.putExtra("albumid", albumid);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_ANCHOR:
                                        //跳转主播专辑主页
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_ANCHORID)) {
                                            String anchorid = uri.getQueryParameter(LocalConstant.ACTIONURL_ANCHORID);
                                            if (anchorid == null) {
                                                return null;
                                            }
                                            intent = new Intent(context, RadioAnchorHomePageActivity.class);
                                            intent.putExtra("anchorid", anchorid);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_ALBUMLIST:
                                        //专辑列表（推荐专辑／最新首发）
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_CATEGORY)) {
                                            String category = uri.getQueryParameter(LocalConstant.ACTIONURL_CATEGORY);
                                            if (category == null) {
                                                return null;
                                            }
                                            intent = new Intent(context, RadioAlbumListActivity.class);
                                            switch (category) {
                                                case LocalConstant.ACTIONURL_TOP:
                                                    //跳转推荐专辑列表
                                                    intent.putExtra("category", LocalConstant.ACTIONURL_TOP);
                                                    intent.putExtra("categoryName", "推荐专辑");
                                                    break;
                                                case LocalConstant.ACTIONURL_NEWEST:
                                                    //跳转最新首发列表
                                                    intent.putExtra("category", LocalConstant.ACTIONURL_NEWEST);
                                                    intent.putExtra("categoryName", "最新首发");
                                                    break;
                                                case LocalConstant.ACTIONURL_MUSIC:
                                                    //跳转儿歌专辑列表
                                                    intent.putExtra("category", LocalConstant.ACTIONURL_MUSIC);
                                                    intent.putExtra("categoryName", "热门儿歌");
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                        break;
                                }
                            }
                            break;
                        case LocalConstant.ACTIONURL_COMMUNITY:
                            if (queryParameterNames.contains(LocalConstant.ACTIONURL_SUBTYPE)) {
                                String subtype = uri.getQueryParameter(LocalConstant.ACTIONURL_SUBTYPE);
                                if (subtype == null) {
                                    return null;
                                }
                                switch (subtype) {
                                    case LocalConstant.ACTIONURL_DETAIL:
                                        //帖子详情
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_TOPICID) && queryParameterNames.contains(LocalConstant.ACTIONURL_FILEPATH)) {
                                            String topicid = uri.getQueryParameter(LocalConstant.ACTIONURL_TOPICID);
                                            String filepath = uri.getQueryParameter(LocalConstant.ACTIONURL_FILEPATH);
                                            if (!AsStrUtils.isEmpty(topicid) && !AsStrUtils.isEmpty(filepath)) {
                                                String webUrl = getWebUrl(context, filepath);
                                                intent = new Intent(context, CommunityPostDetailActivity.class);
                                                intent.putExtra("url", webUrl);
                                                intent.putExtra("topicid", topicid);
//                                                CommunityPostDetailActivity.startActivity(context, webUrl, topicid);
                                            }
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_COLLECTION:
                                        //我的收藏
                                        if (isLogined) {
                                            intent = new Intent(context, MoreCollectionActivity.class);
                                        } else {
                                            intent = new Intent(context, ThirdPartyLoginActivity.class);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_ZONE:
                                        //个人主页
                                        if (isLogined) {
                                            String userid = uri.getQueryParameter("userid");
                                            String username = uri.getQueryParameter("username");
                                            intent = new Intent(context, CommunityPostMyMainPageActivity.class);
                                            intent.putExtra("userid", userid);
                                            intent.putExtra("username", username);
                                        } else {
                                            intent = new Intent(context, ThirdPartyLoginActivity.class);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_REPLY:
                                        //回复
                                        if (isLogined) {
                                            String modularid = uri.getQueryParameter("modularid");
                                            String topicid = uri.getQueryParameter("topicid");
                                            String replyid = uri.getQueryParameter("replyid");
                                            String parentid = uri.getQueryParameter("parentid");
                                            intent = new Intent(context, CommunityPostPublishActivity.class);
                                            intent.putExtra("isReply", true);
                                            intent.putExtra("modularid", modularid);
                                            intent.putExtra("topicid", topicid);
                                            intent.putExtra("replyid", replyid);
                                            intent.putExtra("parentid", parentid);
                                            LogUtil.e("PostDetailActivity", "modularid: " + modularid + ";topicid: " + topicid + ";parentid: " + parentid + ";replyid: " + replyid);
                                        } else {
                                            intent = new Intent(context, ThirdPartyLoginActivity.class);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_IMG:
                                        //浏览图片
                                        String src = uri.getQueryParameter("src");
                                        String index = uri.getQueryParameter("index");
                                        LogUtil.e("PostDetailActivity", "src: " + src + ";index: " + index);
                                        String[] paths = src.split(",");
                                        ArrayList<String> listImage = new ArrayList<>();
                                        for (String pathString : paths) {
                                            listImage.add(pathString);
                                        }
                                        intent = new Intent(context, ImageListBrowseActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable("list", listImage);
                                        intent.putExtras(bundle);
                                        intent.putExtra("position", Integer.parseInt(index));
                                        break;
                                }
                            }
                            break;
                        //个人中心菜单
                        case LocalConstant.ACTIONURL_MULTIPLE:
                            if (queryParameterNames.contains(LocalConstant.ACTIONURL_SUBTYPE)) {
                                String subtype = uri.getQueryParameter(LocalConstant.ACTIONURL_SUBTYPE);
                                if (subtype == null) {
                                    return null;
                                }
                                switch (subtype) {
                                    case LocalConstant.ACTIONURL_USERSCORE:
                                        //积分
                                        if (isLogined) {
                                            intent = new Intent(context, MorePointsActivity.class);
                                        } else {
                                            intent = new Intent(context, ThirdPartyLoginActivity.class);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_DOWNLOAD:
                                        //下载
                                        if (isLogined) {
                                            if (queryParameterNames.contains(LocalConstant.ACTIONURL_CATEGORY)) {
                                                String category = uri.getQueryParameter(LocalConstant.ACTIONURL_CATEGORY);
                                                if (category == null) {
                                                    return null;
                                                }
                                                switch (category) {
                                                    case LocalConstant.ACTIONURL_UNIVERSAL:
                                                        //跳转下载管理
                                                        intent = new Intent(context, DownloadMangerActivity.class);
                                                        break;
                                                    case LocalConstant.ACTIONURL_PICTURE:
                                                        break;
                                                    case LocalConstant.ACTIONURL_MATH:
                                                        break;
                                                    default:
                                                        break;
                                                }
                                            }
                                        } else {
                                            intent = new Intent(context, ThirdPartyLoginActivity.class);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_ABOUTAPP:
                                        // 关于我们
                                        intent = new Intent(context, MoreAboutUsActivity.class);
                                        break;
                                    case LocalConstant.ACTIONURL_HOMEWORK:
                                        // 布置作业
                                        if (isLogined) {
                                            if (queryParameterNames.contains(LocalConstant.ACTIONURL_CATEGORY)) {
                                                String category = uri.getQueryParameter(LocalConstant.ACTIONURL_CATEGORY);
                                                if (category == null) {
                                                    return null;
                                                }
                                                switch (category) {
                                                    case LocalConstant.ACTIONURL_UNIVERSAL:
                                                        intent = new Intent(context, HomeWorkActivity.class);
                                                        break;
                                                    case LocalConstant.ACTIONURL_SINOLOGY:
                                                        break;
                                                    case LocalConstant.ACTIONURL_MATH:
                                                        break;
                                                    case LocalConstant.ACTIONURL_CLASSIC:
                                                        break;
                                                    default:
                                                        break;
                                                }
                                            }
                                        } else {
                                            intent = new Intent(context, ThirdPartyLoginActivity.class);
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_NEWS:
                                        // 消息弹框
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_CATEGORY)) {
                                            String category = uri.getQueryParameter(LocalConstant.ACTIONURL_CATEGORY);
                                            if (category == null) {
                                                return null;
                                            }
                                            intent = new Intent(context, MessageListActivity.class);
                                            switch (category) {
                                                case LocalConstant.ACTIONURL_UNIVERSAL:
                                                    intent.putExtra("type", LocalConstant.ACTIONURL_UNIVERSAL);
                                                    break;
                                                case LocalConstant.ACTIONURL_ACTIVITY:
                                                    intent.putExtra("type", LocalConstant.ACTIONURL_ACTIVITY);
                                                    break;
                                                case LocalConstant.ACTIONURL_HOMEWORK:
                                                    intent.putExtra("type", LocalConstant.ACTIONURL_HOMEWORK);
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }
                                        break;
                                    case LocalConstant.ACTIONURL_BILLBOARD:
                                        // 积分排行榜
                                        if (queryParameterNames.contains(LocalConstant.ACTIONURL_DURATION)) {
                                            String duration = uri.getQueryParameter(LocalConstant.ACTIONURL_DURATION);
                                            if (duration == null) {
                                                return null;
                                            }
                                            intent = new Intent(context, HomeWorkRankActivity.class);
                                            intent.putExtra("duration", LocalConstant.ACTIONURL_DURATION);
                                        }
                                        break;
                                }
                            }
                            break;
                        case LocalConstant.ACTIONURL_SHARE:
                            //分享app
                            if (isLogined) {
                                if (queryParameterNames.contains(LocalConstant.ACTIONURL_TITLE) && queryParameterNames.contains(LocalConstant.ACTIONURL_CONTENT)
                                        && queryParameterNames.contains(LocalConstant.ACTIONURL_URL) && queryParameterNames.contains(LocalConstant.ACTIONURL_IMAGE)) {
                                    String title = uri.getQueryParameter(LocalConstant.ACTIONURL_TITLE);
                                    String content = uri.getQueryParameter(LocalConstant.ACTIONURL_CONTENT);
                                    String url = uri.getQueryParameter(LocalConstant.ACTIONURL_URL);
                                    String image = uri.getQueryParameter(LocalConstant.ACTIONURL_IMAGE);
                                    LogUtil.d("UrlUtil", "showShare");
                                    ShareUtil.showShare(context, title, content, url, image);
                                }
                            } else {
                                intent = new Intent(context, ThirdPartyLoginActivity.class);
                            }
                            break;
                        case LocalConstant.ACTIONURL_EMALL:
                            //积分商城
                            if (queryParameterNames.contains(LocalConstant.ACTIONURL_URL)) {
                                String url = uri.getQueryParameter(LocalConstant.ACTIONURL_URL);
                                if (url == null) {
                                    return null;
                                }
                                String webUrl = getWebUrl(context, url);
                                LogUtil.d("UrlUtil", "webUrl: " + webUrl);
                                if (webUrl == null) {
                                    return null;
                                }
                                if (queryParameterNames.contains(LocalConstant.ACTIONURL_AUTH)) {
                                    String auth = uri.getQueryParameter(LocalConstant.ACTIONURL_AUTH);
                                    if (auth == null) {
                                        return null;
                                    }
                                    if (auth.equals("1")) {
                                        //需要登录
                                        if (MyApplication.spUtil.getBoolPreferenceByParamName(context, LocalConstant.SP_IS_LOGINED)) {
                                            intent = new Intent(context, IntegralMallActivity.class);
                                            intent.putExtra("url", webUrl);
                                        } else {
                                            intent = new Intent(context, ThirdPartyLoginActivity.class);
                                        }
                                    } else {
                                        intent = new Intent(context, IntegralMallActivity.class);
                                        intent.putExtra("url", webUrl);
                                    }
                                }
                            }
                            break;
                        default:
                            break;
                    }
                    if (isAction && intent != null) {
                        context.startActivity(intent);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return intent;
    }

    public static String getWebUrl(Context context, String weburl) {
        String userid = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_USERID);
        String babyid = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_BABYID);
        String version = MyApplication.versionCode;
        String uuid = MyApplication.deviceId;
        String token = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_USER_TOKEN);
        return weburl + String.format(LocalConstant.URL_BASE, userid, babyid, version, uuid, token);
    }

    //启动绘本
    private static void startDrawBook(final Context context, final String bookid) {
        File file = new File(FileUtils.getDrwabookDirDirectory(), bookid + "/");
        if (file.isDirectory() && file.listFiles().length > 0 && isGameNotNull(bookid, 1)) {
            String userid = MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USERID);
            String babyid = MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_BABYID);
            String token = MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USER_TOKEN);
            String uuid = MyApplication.deviceId;
            String version = MyApplication.versionCode;
            String url = String.format(HttpWebCoreService.HUIBEN_URL, bookid, userid,
                    babyid, version, uuid, token, System.currentTimeMillis(), bookid);
            OaGameActivity.startActivity(context, url);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage(context.getString(R.string.toast_local_game_null))
                    .setPositiveButton("去下载", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            DrawBookDetailActivity.startActivity(context, bookid);
                        }
                    })
                    .setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .show();
        }
    }

    //启动游戏
    private static void startGame(final Context context, final String bookid, String lessonid) {
        File file = new File(FileUtils.getGameDirectory(), bookid + "/");
        if (file.isDirectory() && file.listFiles().length > 0 && isGameNotNull(bookid, 2)) {
            String userid = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_USERID);
            String babyid = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_BABYID);
            String token = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_USER_TOKEN);
            String uuid = MyApplication.deviceId;
            String version = MyApplication.versionCode;
            String url = String.format(HttpWebCoreService.GAME_LESSON_URL, bookid, userid,
                    babyid, version, uuid, token, System.currentTimeMillis(), bookid, lessonid);
            OaGameActivity.startActivity(context, url);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage(context.getString(R.string.toast_local_drawbook_null))
                    .setPositiveButton("去下载", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            MathGameDetailActivity.startActivity(context, bookid);
                        }
                    })
                    .setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .show();
        }
    }

    //是否已下载
    private static boolean isGameNotNull(String bookid, int type) {
        boolean result = false;
        List<LocalGmaeInfo> localGmaeInfoList = MyApplication.db.findAllByWhere(LocalGmaeInfo.class, "pid=" + bookid + " and type=" + type);
        if (!localGmaeInfoList.isEmpty()) {
            result = true;
        }
        return result;
    }
}
